home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / m88k-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  17.1 KB  |  601 lines

  1. /* Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  2.  
  3. This file is part of GDB.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <stdio.h>
  20. #include "defs.h"
  21. #include "param.h"
  22. #include "frame.h"
  23. #include "inferior.h"
  24. #include "value.h"
  25.  
  26. #ifdef USG
  27. #include <sys/types.h>
  28. #endif
  29.  
  30. #include <sys/param.h>
  31. #include <sys/dir.h>
  32. #include <signal.h>
  33. #include "gdbcore.h"
  34. #include <sys/user.h>
  35. #ifndef USER            /* added to support BCS ptrace_user */
  36.  
  37. #define USER ptrace_user
  38. #endif
  39. #include <sys/ioctl.h>
  40. #include <fcntl.h>
  41.  
  42. #ifdef COFF_ENCAPSULATE
  43. #include "a.out.encap.h"
  44. #else
  45. #include <a.out.h>
  46. #endif
  47.  
  48. #include <sys/file.h>
  49. #include <sys/stat.h>
  50.  
  51. #include "symtab.h"
  52. #include "setjmp.h"
  53. #include "value.h"
  54.  
  55. int stack_error;
  56. jmp_buf stack_jmp;
  57.  
  58. void
  59. tdesc_error_function  (environment, continuable, message)
  60. dc_word_t environment;
  61. dc_boolean_t continuable;
  62. char *message;
  63. {
  64.   if (stack_error) longjmp (stack_jmp, 1);
  65.   if (!continuable)
  66.     {
  67.       printf("%s\n",message);
  68.       abort();
  69.     }
  70. }
  71.  
  72.  
  73. void
  74. tdesc_read_function (environment, memory, length, buffer)
  75. dc_word_t environment;
  76. dc_word_t memory;
  77. int length;
  78. char *buffer;
  79. {
  80.   int ptrace_code;
  81.   errno = 0;
  82.   if (memory < 2048) 
  83. #if 0
  84.     /* This is a no-op!  It sets buffer, but doesn't do anything to
  85.        what buffer points to.  What does this function do anyway?
  86.        And this is wrong for cross-debugging.  */
  87.     buffer = ptrace (3, inferior_pid, memory, 0);
  88. #else
  89.     return;
  90. #endif
  91.   else
  92.     read_memory (memory, buffer, length);
  93. }
  94.  
  95. /* Map function for tdesc */
  96. void
  97. tdesc_map_function (map_env, loc, map_info_in, map_info_out)
  98. dc_word_t map_env;
  99. dc_word_t loc;
  100. dc_map_info_in_t map_info_in;
  101. dc_map_info_out_t *map_info_out;
  102. {
  103. int map_flags = DC_MIO_ENTRY_POINT | DC_MIO_IMPLICIT_PROLOGUE_END;
  104. int entry_point = get_pc_function_start(loc);
  105. map_info_out->flags = map_flags;
  106. map_info_out->entry_point = entry_point;
  107. }
  108.  
  109. dc_handle_t tdesc_handle;
  110.  
  111. extern int debug_info;
  112.  
  113. void
  114. init_tdesc ()
  115. {
  116.   tdesc_handle = dc_initiate (debug_info, tdesc_error_function,
  117.                   0,tdesc_read_function,0,0,0,0,0,tdesc_map_function,0);
  118. dc_dcontext_t current_context;
  119.   
  120. /* setup current context, called from wait_for_inferior */
  121.  
  122. dc_dcontext_t
  123. init_dcontext()
  124. {
  125.   dc_word_t reg_info[DC_NUM_REG];
  126.   dc_word_t reg_flags[2] = {0,-1};
  127.   dc_word_t aux_info[DC_NUM_AUX];
  128.   dc_word_t aux_flags[2] = {0,-1};
  129.   dc_exactness_t loc_exact = DC_NO;
  130.   dc_word_t psr_info;
  131.   dc_boolean_t psr_ind = 0;
  132.   dc_word_t psr_flags[2] = {0,-1};
  133.  
  134.   bcopy (®isters, reg_info, DC_NUM_REG * 4);
  135.   aux_info[DC_AUX_LOC] =  read_register(SXIP_REGNUM);
  136.   aux_info[DC_AUX_SXIP] = read_register(SXIP_REGNUM);
  137.   aux_info[DC_AUX_SNIP] = read_register(SNIP_REGNUM);
  138.   aux_info[DC_AUX_SFIP] = read_register(SFIP_REGNUM);
  139.   aux_info[DC_AUX_FPSR] = read_register(FPSR_REGNUM);
  140.   aux_info[DC_AUX_FPCR] = read_register(FPCR_REGNUM);
  141.  
  142.   psr_info = read_register(PSR_REGNUM);
  143.  
  144.   return dc_make_dcontext (tdesc_handle, reg_info, reg_flags, aux_info,
  145.                          aux_flags, loc_exact, psr_info, psr_ind, psr_flags);
  146. }
  147.  
  148.  
  149. dc_dcontext_t
  150. get_prev_context (context)
  151.   dc_dcontext_t context;
  152. {
  153.   return current_context = dc_previous_dcontext (context); 
  154. }
  155.   
  156.  
  157.  
  158.  
  159. /* Determine frame base for this file's frames.  This will be either
  160.    the CFA or the old style FP_REGNUM; the symtab for the current pc's
  161.    file has the information                          */
  162.  
  163. CORE_ADDR
  164. get_frame_base(pc)
  165. CORE_ADDR pc;
  166. {
  167.   struct symtab *this_file = find_pc_symtab(pc);
  168.   int coffsem_frame_position;
  169.   
  170.   /* If this_file is null, there's a good chance the file was compiled
  171.      without -g.  If that's the case, use CFA (canonical frame addr) 
  172.      as the default frame pointer.                                 */
  173.  
  174.   if (this_file)
  175.     {
  176.     coffsem_frame_position = this_file->coffsem & 3;
  177.     if (coffsem_frame_position == 1) 
  178.       return (CORE_ADDR) dc_general_register (current_context, FP_REGNUM);
  179.     else
  180.       /* default is CFA, as well as if coffsem==2 */
  181.       return (CORE_ADDR) dc_frame_address  (current_context);
  182.     }
  183.  
  184.   return (CORE_ADDR) dc_frame_address (current_context);
  185. }
  186.  
  187. #if TARGET_BYTE_ORDER != HOST_BYTE_ORDER
  188. you lose
  189. #else /* Host and target byte order the same.  */
  190. #define SINGLE_EXP_BITS  8
  191. #define DOUBLE_EXP_BITS 11
  192. int
  193. IEEE_isNAN(fp, len)
  194.      int *fp, len;
  195.      /* fp points to a single precision OR double precision
  196.       * floating point value; len is the number of bytes, either 4 or 8.
  197.       * Returns 1 iff fp points to a valid IEEE floating point number.
  198.       * Returns 0 if fp points to a denormalized number or a NaN
  199.       */
  200. {
  201.   int exponent;
  202.   if (len == 4)
  203.     {
  204.       exponent = *fp;
  205.       exponent = exponent << 1 >> (32 - SINGLE_EXP_BITS - 1);
  206.       return ((exponent == -1) || (! exponent && *fp));
  207.     }
  208.   else if (len == 8)
  209.     {
  210.       exponent = *(fp+1);
  211.       exponent = exponent << 1 >> (32 - DOUBLE_EXP_BITS - 1);
  212.       return ((exponent == -1) || (! exponent && *fp * *(fp+1)));
  213.     }
  214.   else return 1;
  215. }
  216. #endif /* Host and target byte order the same.  */
  217.  
  218. #define FIRST_PRESERVED_REGNUM    14
  219. #define LAST_PRESERVED_REGNUM    25
  220. #define FIRST_PARM_REGNUM    2
  221. #define LAST_PARM_REGNUM    9
  222.  
  223. #define MAX_REG_PARMS    (LAST_PARM_REGNUM - FIRST_PARM_REGNUM + 1)
  224.  
  225. void
  226. frame_find_saved_regs (fi, fsr)
  227.       struct frame_info *fi;
  228.       struct frame_saved_regs *fsr;
  229. {
  230.    register int regnum;
  231.  
  232.    error ("Feature not implemented for the 88k yet.");
  233.    return;
  234.  
  235. #if 0
  236.    for (regnum = FIRST_PARM_REGNUM; regnum <= LAST_PARM_REGNUM; regnum++)
  237.      fsr->regs[regnum]
  238.         = (unsigned) fi->frame - ((regnum - FIRST_PARM_REGNUM) * 4);
  239.  
  240.    fsr->regs[SP_REGNUM] = 0;        /* SP not saved in frames */
  241.    fsr->regs[FP_REGNUM] = fi->frame;
  242.    fsr->regs[PC_REGNUM] = fi->frame + 4;
  243. #endif
  244. }
  245.  
  246. static int
  247. pushed_size (prev_words, v)
  248.      int prev_words;
  249.      struct value *v;
  250. {
  251.   switch (TYPE_CODE (VALUE_TYPE (v)))
  252.     {
  253.       case TYPE_CODE_VOID:        /* Void type (values zero length) */
  254.  
  255.     return 0;    /* That was easy! */
  256.  
  257.       case TYPE_CODE_PTR:        /* Pointer type */
  258.       case TYPE_CODE_ENUM:        /* Enumeration type */
  259.       case TYPE_CODE_INT:        /* Integer type */
  260.       case TYPE_CODE_REF:        /* C++ Reference types */
  261.       case TYPE_CODE_ARRAY:        /* Array type, lower bound zero */
  262.  
  263.     return 1;
  264.  
  265.       case TYPE_CODE_FLT:        /* Floating type */
  266.  
  267.     if (TYPE_LENGTH (VALUE_TYPE (v)) == 4)
  268.       return 1;
  269.     else
  270.       /* Assume that it must be a double.  */
  271.       if (prev_words & 1)        /* at an odd-word boundary */
  272.         return 3;            /* round to 8-byte boundary */
  273.       else
  274.         return 2;
  275.  
  276.       case TYPE_CODE_STRUCT:        /* C struct or Pascal record */
  277.       case TYPE_CODE_UNION:        /* C union or Pascal variant part */
  278.  
  279.     return (((TYPE_LENGTH (VALUE_TYPE (v)) + 3) / 4) * 4);
  280.  
  281.       case TYPE_CODE_FUNC:        /* Function type */
  282.       case TYPE_CODE_SET:        /* Pascal sets */
  283.       case TYPE_CODE_RANGE:        /* Range (integers within bounds) */
  284.       case TYPE_CODE_PASCAL_ARRAY:    /* Array with explicit type of index */
  285.       case TYPE_CODE_MEMBER:        /* Member type */
  286.       case TYPE_CODE_METHOD:        /* Method type */
  287.     /* Don't know how to pass these yet.  */
  288.  
  289.       case TYPE_CODE_UNDEF:        /* Not used; catches errors */
  290.       default:
  291.     abort ();
  292.     }
  293. }
  294.  
  295. static void
  296. store_parm_word (address, val)
  297.      CORE_ADDR address;
  298.      int val;
  299. {
  300.   write_memory (address, &val, 4);
  301. }
  302.  
  303. static int
  304. store_parm (prev_words, left_parm_addr, v)
  305.      unsigned int prev_words;
  306.      CORE_ADDR left_parm_addr;
  307.      struct value *v;
  308. {
  309.   CORE_ADDR start = left_parm_addr + (prev_words * 4);
  310.   int *val_addr = (int *)VALUE_CONTENTS(v);
  311.  
  312.   switch (TYPE_CODE (VALUE_TYPE (v)))
  313.     {
  314.       case TYPE_CODE_VOID:        /* Void type (values zero length) */
  315.  
  316.     return 0;
  317.  
  318.       case TYPE_CODE_PTR:        /* Pointer type */
  319.       case TYPE_CODE_ENUM:        /* Enumeration type */
  320.       case TYPE_CODE_INT:        /* Integer type */
  321.       case TYPE_CODE_ARRAY:        /* Array type, lower bound zero */
  322.       case TYPE_CODE_REF:        /* C++ Reference types */
  323.  
  324.     store_parm_word (start, *val_addr);
  325.     return 1;
  326.  
  327.       case TYPE_CODE_FLT:        /* Floating type */
  328.  
  329.     if (TYPE_LENGTH (VALUE_TYPE (v)) == 4)
  330.       {
  331.         store_parm_word (start, *val_addr);
  332.         return 1;
  333.       }
  334.     else
  335.       {
  336.         store_parm_word (start + ((prev_words & 1) * 4), val_addr[0]);
  337.         store_parm_word (start + ((prev_words & 1) * 4) + 4, val_addr[1]);
  338.         return 2 + (prev_words & 1);
  339.       }
  340.  
  341.       case TYPE_CODE_STRUCT:        /* C struct or Pascal record */
  342.       case TYPE_CODE_UNION:        /* C union or Pascal variant part */
  343.  
  344.     {
  345.       unsigned int words = (((TYPE_LENGTH (VALUE_TYPE (v)) + 3) / 4) * 4);
  346.       unsigned int word;
  347.  
  348.       for (word = 0; word < words; word++)
  349.         store_parm_word (start + (word * 4), val_addr[word]);
  350.       return words;
  351.     }
  352.  
  353.       default:
  354.     abort ();
  355.     }
  356. }
  357.  
  358.  /* This routine sets up all of the parameter values needed to make a pseudo
  359.     call.  The name "push_parameters" is a misnomer on some archs,
  360.     because (on the m88k) most parameters generally end up being passed in
  361.     registers rather than on the stack.  In this routine however, we do
  362.     end up storing *all* parameter values onto the stack (even if we will
  363.     realize later that some of these stores were unnecessary).  */
  364.  
  365. void
  366. push_parameters (return_type, struct_conv, nargs, args)
  367.       struct type *return_type; 
  368.       int struct_conv;
  369.       int nargs;
  370.       value *args;
  371.  {
  372.    int parm_num;
  373.    unsigned int p_words = 0;
  374.    CORE_ADDR left_parm_addr;
  375.  
  376.    /* Start out by creating a space for the return value (if need be).  We
  377.       only need to do this if the return value is a struct or union.  If we
  378.       do make a space for a struct or union return value, then we must also
  379.       arrange for the base address of that space to go into r12, which is the
  380.       standard place to pass the address of the return value area to the
  381.       callee.  Note that only structs and unions are returned in this fashion.
  382.       Ints, enums, pointers, and floats are returned into r2.  Doubles are
  383.       returned into the register pair {r2,r3}.  Note also that the space
  384.       reserved for a struct or union return value only has to be word aligned
  385.       (not double-word) but it is double-word aligned here anyway (just in
  386.       case that becomes important someday).  */
  387.  
  388.    switch (TYPE_CODE (return_type))
  389.      {
  390.        case TYPE_CODE_STRUCT:
  391.        case TYPE_CODE_UNION:
  392.          {
  393.            int return_bytes = ((TYPE_LENGTH (return_type) + 7) / 8) * 8;
  394.            CORE_ADDR rv_addr;
  395.  
  396.            rv_addr = read_register (SP_REGNUM) - return_bytes;
  397.  
  398.            write_register (SP_REGNUM, rv_addr); /* push space onto the stack */
  399.            write_register (SRA_REGNUM, rv_addr);/* set return value register */
  400.          }
  401.      }
  402.  
  403.    /* Here we make a pre-pass on the whole parameter list to figure out exactly
  404.       how many words worth of stuff we are going to pass.  */
  405.  
  406.    for (p_words = 0, parm_num = 0; parm_num < nargs; parm_num++)
  407.      p_words += pushed_size (p_words, value_arg_coerce (args[parm_num]));
  408.  
  409.    /* Now, check to see if we have to round up the number of parameter words
  410.       to get up to the next 8-bytes boundary.  This may be necessary because
  411.       of the software convention to always keep the stack aligned on an 8-byte
  412.       boundary.  */
  413.  
  414.    if (p_words & 1)
  415.      p_words++;        /* round to 8-byte boundary */
  416.  
  417.    /* Now figure out the absolute address of the leftmost parameter, and update
  418.       the stack pointer to point at that address.  */
  419.  
  420.    left_parm_addr = read_register (SP_REGNUM) - (p_words * 4);
  421.    write_register (SP_REGNUM, left_parm_addr);
  422.  
  423.    /* Now we can go through all of the parameters (in left-to-right order)
  424.       and write them to their parameter stack slots.  Note that we are not
  425.       really "pushing" the parameter values.  The stack space for these values
  426.       was already allocated above.  Now we are just filling it up.  */
  427.  
  428.    for (p_words = 0, parm_num = 0; parm_num < nargs; parm_num++)
  429.      p_words +=
  430.        store_parm (p_words, left_parm_addr, value_arg_coerce (args[parm_num]));
  431.  
  432.    /* Now that we are all done storing the parameter values into the stack, we
  433.       must go back and load up the parameter registers with the values from the
  434.       corresponding stack slots.  Note that in the two cases of (a) gaps in the
  435.       parameter word sequence causes by (otherwise) misaligned doubles, and (b)
  436.       slots correcponding to structs or unions, the work we do here in loading
  437.       some parameter registers may be unnecessary, but who cares?  */
  438.  
  439.    for (p_words = 0; p_words < 8; p_words++)
  440.      {
  441.        write_register (FIRST_PARM_REGNUM + p_words,
  442.          read_memory_integer (left_parm_addr + (p_words * 4), 4));
  443.      }
  444. }
  445.  
  446. void
  447. pop_frame ()
  448. {
  449.   error ("Feature not implemented for the m88k yet.");
  450.   return;
  451. }
  452.  
  453.  void
  454.  collect_returned_value (rval, value_type, struct_return, nargs, args)
  455.       value *rval;
  456.       struct type *value_type;
  457.       int struct_return;
  458.       int nargs;
  459.       value *args;
  460.  {
  461.    char retbuf[REGISTER_BYTES];
  462.  
  463.    bcopy (registers, retbuf, REGISTER_BYTES);
  464.    *rval = value_being_returned (value_type, retbuf, struct_return);
  465.    return;
  466.  }
  467.  
  468. #if 0
  469. /* Now handled in a machine independent way with CALL_DUMMY_LOCATION.  */
  470.  /* Stuff a breakpoint instruction onto the stack (or elsewhere if the stack
  471.     is not a good place for it).  Return the address at which the instruction
  472.     got stuffed, or zero if we were unable to stuff it anywhere.  */
  473.   
  474.  CORE_ADDR
  475.  push_breakpoint ()
  476.   {
  477.    static char breakpoint_insn[] = BREAKPOINT;
  478.    extern CORE_ADDR text_end;    /* of inferior */
  479.    static char readback_buffer[] = BREAKPOINT;
  480.    int i;
  481.   
  482.    /* With a little bit of luck, we can just stash the breakpoint instruction
  483.       in the word just beyond the end of normal text space.  For systems on
  484.       which the hardware will not allow us to execute out of the stack segment,
  485.       we have to hope that we *are* at least allowed to effectively extend the
  486.       text segment by one word.  If the actual end of user's the text segment
  487.       happens to fall right at a page boundary this trick may fail.  Note that
  488.       we check for this by reading after writing, and comparing in order to
  489.       be sure that the write worked.  */
  490.  
  491.    write_memory (text_end, &breakpoint_insn, 4);
  492.  
  493.    /* Fill the readback buffer with some garbage which is certain to be
  494.       unequal to the breakpoint insn.  That way we can tell if the
  495.       following read doesn't actually succeed.  */
  496.  
  497.    for (i = 0; i < sizeof (readback_buffer); i++)
  498.      readback_buffer[i] = ~ readback_buffer[i];    /* Invert the bits */
  499.  
  500.    /* Now check that the breakpoint insn was successfully installed.  */
  501.  
  502.    read_memory (text_end, readback_buffer, sizeof (readback_buffer));
  503.    for (i = 0; i < sizeof (readback_buffer); i++)
  504.      if (readback_buffer[i] != breakpoint_insn[i])
  505.        return 0;        /* Failed to install! */
  506.  
  507.    return text_end;
  508.  }
  509. #endif
  510.  
  511. /* Like dc_psr_register but takes an extra int arg.  */
  512. static dc_word_t
  513. psr_register (context, dummy)
  514.      dc_dcontext_t context;
  515.      int dummy;
  516. {
  517.   return dc_psr_register (context);
  518. }
  519.  
  520. /* Same functionality as get_saved_register in findvar.c, but implemented
  521.    to use tdesc.  */
  522. void
  523. get_saved_register (raw_buffer, optim, addrp, frame, regnum, lvalp)
  524.      char *raw_buffer;
  525.      int *optim;
  526.      CORE_ADDR *addrp;
  527.      FRAME frame;
  528.      int regnum;
  529.      enum lval_type *lvalp;
  530. {
  531.   struct frame_info *fi = get_frame_info (frame);
  532.   
  533.   /* Functions to say whether a register is optimized out, and
  534.      if not, to get the value.  Take as args a context and the
  535.      value of get_reg_arg.  */
  536.   int (*get_reg_state) ();
  537.   dc_word_t (*get_reg) ();
  538.   int get_reg_arg;
  539.  
  540.   /* Because tdesc doesn't tell us whether it got it from a register
  541.      or memory, always say we don't have an address for it.  */
  542.   if (addrp != NULL)
  543.     *addrp = 0;
  544.   
  545.   if (regnum < DC_NUM_REG)
  546.     {
  547.       get_reg_state = dc_general_register_state;
  548.       get_reg = dc_general_register;
  549.       get_reg_arg = regnum;
  550.     }
  551.   else
  552.     {
  553.       get_reg_state = dc_auxiliary_register_state;
  554.       get_reg = dc_auxiliary_register;
  555.       switch (regnum)
  556.     {
  557.     case SXIP_REGNUM:
  558.       get_reg_arg = DC_AUX_SXIP;
  559.       break;
  560.     case SNIP_REGNUM:
  561.       get_reg_arg = DC_AUX_SNIP;
  562.       break;
  563.     case FPSR_REGNUM:
  564.       get_reg_arg = DC_AUX_FPSR;
  565.       break;
  566.     case FPCR_REGNUM:
  567.       get_reg_arg = DC_AUX_FPCR;
  568.       break;
  569.     case PSR_REGNUM:
  570.       get_reg_state = dc_psr_register_bit_state;
  571.       get_reg = psr_register;
  572.       get_reg_arg = 0;
  573.       break;
  574.     default:
  575.       if (optim != NULL)
  576.         *optim = 1;
  577.       return;
  578.     }
  579.     }
  580.  
  581.   if ((*get_reg_state) (fi->frame_context, get_reg_arg))
  582.     {
  583.       if (raw_buffer != NULL)
  584.     *(int *)raw_buffer = (*get_reg) (fi->frame_context, get_reg_arg);
  585.       if (optim != NULL)
  586.     *optim = 0;
  587.       return;
  588.     }
  589.   else
  590.     {
  591.       if (optim != NULL)
  592.     *optim = 1;
  593.       return;
  594.     }
  595.  
  596.   /* Well, the caller can't treat it as a register or memory...  */
  597.   if (lvalp != NULL)
  598.     *lvalp = not_lval;
  599. }
  600.